/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.WebIDE.ejb.sessions;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.UserTransaction;
import org.WebIDE.ejb.entities.*;
import org.WebIDE.ejb.sessions.exceptions.*;

/**
 *
 * @author huy
 */
@Stateless
public class LanguagesFacade implements LanguagesFacadeLocal, LanguagesFacadeRemote {
    @PersistenceContext(unitName = "WebIDE-ejbPU")
    private EntityManager _em;
    @Resource
    private UserTransaction utx = null;
/*
    public void create(Languages languages) {
        _em.persist(languages);
    }

    public void edit(Languages languages) {
        _em.merge(languages);
    }
*/
    public void remove(Languages languages) {
        _em.remove(_em.merge(languages));
    }

    public Languages find(Object id) {
        return _em.find(Languages.class, id);
    }

    public List<Languages> findAll() {
        return _em.createQuery("select object(o) from Languages as o").getResultList();
    }


    public EntityManager getEntityManager() {
        return _em;
    }

    public void create(Languages languages) throws PreexistingEntityException, RollbackFailureException, Exception {
        if (languages.getProjectsCollection() == null) {
            languages.setProjectsCollection(new ArrayList<Projects>());
        }
        EntityManager em = null;
        try {
            //utx.begin();
            em = getEntityManager();
            Collection<Projects> attachedProjectsCollection = new ArrayList<Projects>();
            for (Projects projectsCollectionProjectsToAttach : languages.getProjectsCollection()) {
                projectsCollectionProjectsToAttach = em.getReference(projectsCollectionProjectsToAttach.getClass(), projectsCollectionProjectsToAttach.getId());
                attachedProjectsCollection.add(projectsCollectionProjectsToAttach);
            }
            languages.setProjectsCollection(attachedProjectsCollection);
            em.persist(languages);
            for (Projects projectsCollectionProjects : languages.getProjectsCollection()) {
                Languages oldLanguageOfProjectsCollectionProjects = projectsCollectionProjects.getLanguage();
                projectsCollectionProjects.setLanguage(languages);
                projectsCollectionProjects = em.merge(projectsCollectionProjects);
                if (oldLanguageOfProjectsCollectionProjects != null) {
                    oldLanguageOfProjectsCollectionProjects.getProjectsCollection().remove(projectsCollectionProjects);
                    oldLanguageOfProjectsCollectionProjects = em.merge(oldLanguageOfProjectsCollectionProjects);
                }
            }
            //utx.commit();
        } catch (Exception ex) {
            try {
                //utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findLanguages(languages.getId()) != null) {
                throw new PreexistingEntityException("Languages " + languages + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                //em.close();
            }
        }
    }

    public void edit(Languages languages) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            //utx.begin();
            em = getEntityManager();
            Languages persistentLanguages = em.find(Languages.class, languages.getId());
            Collection<Projects> projectsCollectionOld = persistentLanguages.getProjectsCollection();
            Collection<Projects> projectsCollectionNew = languages.getProjectsCollection();
            Collection<Projects> attachedProjectsCollectionNew = new ArrayList<Projects>();
            for (Projects projectsCollectionNewProjectsToAttach : projectsCollectionNew) {
                projectsCollectionNewProjectsToAttach = em.getReference(projectsCollectionNewProjectsToAttach.getClass(), projectsCollectionNewProjectsToAttach.getId());
                attachedProjectsCollectionNew.add(projectsCollectionNewProjectsToAttach);
            }
            projectsCollectionNew = attachedProjectsCollectionNew;
            languages.setProjectsCollection(projectsCollectionNew);
            languages = em.merge(languages);
            for (Projects projectsCollectionOldProjects : projectsCollectionOld) {
                if (!projectsCollectionNew.contains(projectsCollectionOldProjects)) {
                    projectsCollectionOldProjects.setLanguage(null);
                    projectsCollectionOldProjects = em.merge(projectsCollectionOldProjects);
                }
            }
            for (Projects projectsCollectionNewProjects : projectsCollectionNew) {
                if (!projectsCollectionOld.contains(projectsCollectionNewProjects)) {
                    Languages oldLanguageOfProjectsCollectionNewProjects = projectsCollectionNewProjects.getLanguage();
                    projectsCollectionNewProjects.setLanguage(languages);
                    projectsCollectionNewProjects = em.merge(projectsCollectionNewProjects);
                    if (oldLanguageOfProjectsCollectionNewProjects != null && !oldLanguageOfProjectsCollectionNewProjects.equals(languages)) {
                        oldLanguageOfProjectsCollectionNewProjects.getProjectsCollection().remove(projectsCollectionNewProjects);
                        oldLanguageOfProjectsCollectionNewProjects = em.merge(oldLanguageOfProjectsCollectionNewProjects);
                    }
                }
            }
            //utx.commit();
        } catch (Exception ex) {
            try {
                //utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = languages.getId();
                if (findLanguages(id) == null) {
                    throw new NonexistentEntityException("The languages with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                //em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            //utx.begin();
            em = getEntityManager();
            Languages languages;
            try {
                languages = em.getReference(Languages.class, id);
                languages.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The languages with id " + id + " no longer exists.", enfe);
            }
            Collection<Projects> projectsCollection = languages.getProjectsCollection();
            for (Projects projectsCollectionProjects : projectsCollection) {
                projectsCollectionProjects.setLanguage(null);
                projectsCollectionProjects = em.merge(projectsCollectionProjects);
            }
            em.remove(languages);
            //utx.commit();
        } catch (Exception ex) {
            try {
                //utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                //em.close();
            }
        }
    }

    public List<Languages> findLanguagesEntities() {
        return findLanguagesEntities(true, -1, -1);
    }

    public List<Languages> findLanguagesEntities(int maxResults, int firstResult) {
        return findLanguagesEntities(false, maxResults, firstResult);
    }

    private List<Languages> findLanguagesEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Languages as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            //em.close();
        }
    }

    public Languages findLanguages(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Languages.class, id);
        } finally {
            //em.close();
        }
    }

    public int getLanguagesCount() {
        EntityManager em = getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from Languages as o").getSingleResult()).intValue();
        } finally {
            //em.close();
        }
    }

}
